package main

import (
	"fmt"
	"strconv"
	"strings"
	"time"
)

/*
i18n
locale本地化

s1, _ := template.ParseFiles("views/"+lang+"/index.tpl")
VV.Lang=lang
s1.Execute(os.Stdout, VV)

// js文件
<script type="text/javascript" src="views/{{.Lang}}/js/jquery/jquery-1.8.0.min.js"></script>
// css文件
<link href="views/{{.Lang}}/css/bootstrap-responsive.min.css" rel="stylesheet">
// 图片文件
<img src="views/{{.Lang}}/images/btn.png">
*/
var locales map[string]map[string]string

func main() {
	locales = make(map[string]map[string]string, 2)
	en := make(map[string]string, 10)
	en["pea"] = "pea"
	en["bean"] = "bean"
	en["how old"] = "I am %d years old"
	en["time_zone"] = "America/Chicago"
	en["date_format"] = "%Y-%m-%d %H:%M:%S"
	en["money"] = "USD %.2f"
	locales["en"] = en
	cn := make(map[string]string, 10)
	cn["pea"] = "豌豆"
	cn["bean"] = "毛豆"
	cn["how old"] = "我今年%d岁了"
	cn["time_zone"] = "Asia/Shanghai"
	cn["date_format"] = "%Y年%m月%d日 %H时%M分%S秒"
	cn["money"] = "￥%.2f元"
	locales["zh-CN"] = cn
	lang := "zh-CN"
	fmt.Println(msg(lang, "pea"))
	fmt.Println(msg(lang, "bean"))
	fmt.Printf(msg(lang, "how old"), 18)
	fmt.Println()

	loc, _ := time.LoadLocation(msg(lang, "time_zone"))
	t := time.Now()
	t = t.In(loc)
	fmt.Println(t.Format(time.RFC3339))

	fmt.Println(date(msg(lang, "date_format"), t))

	fmt.Println(money_format(msg(lang, "money"), 100.5))
}

func msg(locale, key string) string {
	if v, ok := locales[locale]; ok {
		if v2, ok := v[key]; ok {
			return v2
		}
	}
	return ""
}

func date(format string, t time.Time) string {
	year, month, day := t.Date()
	hour, min, sec := t.Clock()
	format = strings.Replace(format, "%Y", strconv.Itoa(year), -1)
	format = strings.Replace(format, "%m", strconv.FormatUint(uint64(month), 10), -1)
	format = strings.Replace(format, "%d", strconv.Itoa(day), -1)
	format = strings.Replace(format, "%H", strconv.Itoa(hour), -1)
	format = strings.Replace(format, "%M", strconv.Itoa(min), -1)
	format = strings.Replace(format, "%S", strconv.Itoa(sec), -1)
	return format
}

func money_format(format string, money float64) string {
	return fmt.Sprintf(format, money)
}
